import {
  deleteDeviceModel,
  getDeviceModelId,
  postDeviceModel,
  putDeviceModel,
  putDeviceModelName
} from '@/base/api/DeviceModelApi';
import {deepAssign} from '@/base/util/Objects';
import {getDeviceSort} from '@/base/api/DeviceSortApi';
import {Page} from '@/base/Page';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Company} from '@/base/model/Company';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel, DeviceModelName} from '@/base/model/DeviceModel';
import {
  getProductCategoryList,
  getProductColorList,
  getProductId,
  getProductModelList,
  postProduct, putProduct
} from '@/base/api/LvApi';
import json = Mocha.reporters.json;

export class FormBloc {
  device: any = [];
  model: any = {
    id: 0 || undefined,
    categoryId: 0,
    colorId: 0,
    modelId: 0,
    price: 0,
    point: 0,
    banners: '',
    detail: ''
  };
  sortList: any = [];
  modelList: any = [];
  colorList: any = [];
  loading = false;
  fileList: Array<any> = [];
  posterFileList: Array<any> = [];

  num: number = 0;
  sortId: number = 0;
  modelId: number = 0;
  isDetail = false;

  getAllCategory(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getProductCategoryList({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.productCategoryId;
        obj.sortName = item.categoryName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllModel(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getProductModelList({
    }).then((res) => {
      // that.sortPage = res.data;
      this.modelList = res.data.items.map(function (item) {
        var obj = { id: 0, name: '' };
        // @ts-ignore
        obj.id = item.modelId;
        obj.name = item.modelName;
        return obj;
      });
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllColor(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getProductColorList({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      // that.sortPage = res.data;
      this.colorList = res.data.items.map(function (item) {
        var obj = { id: 0, name: '' };
        // @ts-ignore
        obj.id = item.colorId;
        obj.name = item.colorName;
        return obj;
      });
      return this.colorList;
    }).finally(() => {
      this.loading = false;
    });
  }

  addModel(model) {
    this.loading = true;
    return postProduct({
      modelId: model.modelId,
      categoryId: model.categoryId,
      colorId: model.colorId,
      price: model.price,
      point: model.point,
      banners: JSON.stringify(this.posterFileList),
      detail: model.detail
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  getModel(id: number) {
    this.loading = true;
    return getProductId({id}).then((res) => {
      this.model = res.data;
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  // deviceModelName: Array<any> = []
  //
  // putDeviceModelGather() {
  //   return putDeviceModelName().then((res) => {
  //     this.deviceModelName = res.data
  //   })
  // }

  editModel(model: any) {
    return putProduct({
      id: model.id,
      modelId: model.modelId,
      categoryId: model.categoryId,
      colorId: model.colorId,
      price: model.price,
      point: model.point,
      banners: JSON.stringify(this.posterFileList),
      detail: model.detail
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

}
